package Elemenets;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import util.GetDevice;
import util.UIHelper;

/**
 * Created by zhe.xiao on 2017/7/24.
 */

public class HomeElements {
    private static HomeElements homeElements;
    private UiDevice device = GetDevice.getDevice();
    private XiaoRuKou xiaoRuKou;
    private RecomendTime recomendTime;
    private GuessYouGo guessYouGo;
    private HotDestination hotDestination;
    private HotSpecial hotSpecial;
    private GuangGao guangGao;

    private HomeElements() {
    }

    public static HomeElements getInstance() {
        if (homeElements == null) {
            homeElements = new HomeElements();
        }
        return homeElements;
    }

    public GuangGao getGuangGao() {
        if (guangGao == null) {
            guangGao = GuangGao.getInstent(device);
        }
        return guangGao;
    }

    public HotSpecial getHotSpecial() {
        if (hotSpecial == null) {
            hotSpecial = HotSpecial.getInstent(device);
        }
        return hotSpecial;
    }

    public HotDestination getHotDestination() {
        if (hotDestination == null) {
            hotDestination = HotDestination.getInstent(device);
        }
        return hotDestination;
    }

    public GuessYouGo getGuessYouGo() {
        if (guessYouGo == null) {
            guessYouGo = GuessYouGo.getInstent(device);
        }
        return guessYouGo;
    }

    public RecomendTime getRecomendTime() {
        if (recomendTime == null) {
            recomendTime = RecomendTime.getInstent(device);
        }
        return recomendTime;
    }

    public XiaoRuKou getXiaoRuKou() {
        if (xiaoRuKou == null) {
            xiaoRuKou = XiaoRuKou.getInstent(device);
        }
        return xiaoRuKou;
    }

    public UiObject2 getHomeSwipElement() {
        UiObject2 u2 = device.findObject(By.res("android:id/list"));
        return u2;
    }

    /**
     * 进入消息中心的按钮（push）
     *
     * @return
     */
    public UiObject2 getMessage() {
        UiObject2 u = null;
        u = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_push_msg"));
        return u;
    }

    /**
     * 进入搜索的按钮
     *
     * @return
     */
    public UiObject2 getSearch() {
        UiObject2 u2 = null;
        u2 = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_edittext"));
        return u2;
    }

    /**
     * 发现按钮
     *
     * @return
     */
    public UiObject2 getDiscover() {
        UiObject2 u2 = null;
        u2 = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_icon_find_imageview"));
        return u2;
    }

    /**
     * 获取banner
     *
     * @return
     */
    public UiObject2 getBanner() {
//        UiObject2 u2 = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_banner_imageview"));
        UiObject2 u2 = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_pager"));
        return u2;
    }

    /**
     * 获取banner的数量
     *
     * @return
     */
    public int getBannerNum() {
        UiObject2 u2 = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_indicator")).findObject(By.clazz("android.widget.LinearLayout"));
        int i = u2.getChildCount();
        return i;
    }

    /**
     * 小入口模块
     */
    public static class XiaoRuKou {
        public static XiaoRuKou xiaoRuKou;
        private UiDevice device;

        private XiaoRuKou(UiDevice device) {
            this.device = device;
        }

        public void init() {
            UiObject2 pa = this.getParent();
            UIHelper.getElementsAction().swipToElement(this.getBy(), 0.5f);
            UIHelper.getElementsAction().swipContent(pa);
        }

        public static XiaoRuKou getInstent(UiDevice device) {
            if (xiaoRuKou == null) {
                xiaoRuKou = new XiaoRuKou(device);
            }
            return xiaoRuKou;
        }

        public BySelector getBy() {
            return By.res("com.mqunar.atom.alexhome:id/atom_alexhome_small_entrance_card");
        }

        public UiObject2 getParent() {
            UiObject2 u2 = device.wait(Until.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_small_entrance_card")), 20000);
            return u2;
        }

        public UiObject2 getElement1() {
            UiObject2 u2 = this.getParent().getChildren().get(0);
            return u2;
        }

    }

    /**
     * 最近关注模块
     */
    public static class RecomendTime {
        private static RecomendTime recomendTime;
        private UiDevice device;

        private RecomendTime(UiDevice device) {
            this.device = device;
        }

        /**
         * @param device
         * @return
         */
        public static RecomendTime getInstent(UiDevice device) {
            if (recomendTime == null) {
                recomendTime = new RecomendTime(device);
            }
            return recomendTime;
        }

        public void init() {
            UIHelper.getElementsAction().swipToElement(getBy(), 1f);
            UIHelper.getElementsAction().swipContent(getParent());
        }

        public BySelector getBy() {
            return By.text("最近关注");
        }

        public UiObject2 getParent() {
            UiObject2 u2 = device.findObject(getBy()).getParent().getParent().getParent();
            return u2;
        }

        /**
         * 获取元素的属性标签
         * 第一个是类型标签如： 酒店 机票 门票
         * 第二个是名称
         * 第三个是价格
         * 第四个是分数或出发时间
         *
         * @param u2
         * @return
         */
        public List<String> getTag(UiObject2 u2) {
            List<String> l = new ArrayList<String>();
            String t = u2.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_label")).getText();
            String n = u2.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_title")).getText().split("]")[1];
            List<UiObject2> u = u2.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_price_container")).getChildren();
            String p = u.get(0).getText() + u.get(1).getText() + u.get(2).getText();
            l.add(0, t.trim());
            l.add(1, n.trim());
            l.add(2, p.trim());
            if (u2.getChildren().size() == 5) {
                String nn = u2.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_recent_product_salecount")).getText();
                l.add(3, nn.trim());
            }
            return l;
        }

        /**
         * 获取第一个最近关注元素
         *
         * @return
         */
        public UiObject2 get1Ele() {
            UiObject2 u2 = getParent().getChildren().get(1).getChildren().get(0).getChildren().get(0);
            return u2;
        }

        /**
         * 获取第二个最近关注元素
         *
         * @return
         */
        public UiObject2 get2Ele() {
            UiObject2 u2 = getParent().getChildren().get(1).getChildren().get(1);
            return u2;
        }

        public UiObject2 getMore() {
            UiObject2 u = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_card_title_more"));
            return u;
        }
    }

    /**
     * 您可能想去模块
     */
    public static class GuessYouGo {
        private static GuessYouGo guessYouGo;
        private UiDevice device;

        private GuessYouGo(UiDevice device) {
            this.device = device;
        }

        public static GuessYouGo getInstent(UiDevice device) {
            if (guessYouGo == null) {
                guessYouGo = new GuessYouGo(device);
            }
            return guessYouGo;
        }

        public void init() {
            UIHelper.getElementsAction().swipToElement(getBy(), 0.5f);
            UIHelper.getElementsAction().swipContent(getParent());
        }

        public BySelector getBy() {
            return By.text("您可能想去");
        }

        /**
         * 获取您可能想去的父元素
         *
         * @return
         */
        public UiObject2 getParent() {
            UiObject2 u2 = device.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_search_card_container"));
            return u2;
        }

        /**
         * 获取您可能想去文本
         *
         * @return
         */
        public String getTitle() {
            UiObject2 u2 = device.findObject(By.text("您可能想去"));
            String title = u2.getText();
            return title;
        }

        /**
         * 获取3个tag i的值 是 0 1 2
         *
         * @param i
         * @return
         */
        public UiObject2 getTagElement(int i) {
            UiObject2 u2 = getParent().getChildren().get(1);
            return u2.getChildren().get(0).getChildren().get(i);
        }

        /**
         * 获取tag的数量
         *
         * @return
         */
        public int getTagNum() {
            return getParent().getChildren().get(1).getChildren().get(0).getChildren().size();
        }

        /**
         * 获取全部按钮
         *
         * @return
         */
        public UiObject2 getMore() {
            UiObject2 u2 = getParent().getChildren().get(3);
            return u2.findObject(By.res("com.mqunar.atom.alexhome:id/atom_alexhome_poi_more"));
        }

        /**
         * 获取tag城市名
         *
         * @param i
         * @return
         */
        public String getCity(int i) {
            return getTagElement(i).getText().trim();
        }

        /**
         * 获取推荐单品的父对象
         *
         * @return
         */
        public UiObject2 getImgs() {
            UiObject2 u2 = getParent().getChildren().get(4);
            return u2;
        }

        /**
         * 获取第一类单品
         *
         * @return
         */
        public UiObject2 getImg1Ele() {
            UiObject2 u2 = getImgs().getChildren().get(0).getChildren().get(0);
            return u2;
        }

        /**
         * 获取第一类单品的详情
         * list 的第一个是 标题 第二个是 名称 第三个是 副标题 第四个是价格
         *
         * @param u2
         * @return
         */
        public List<String> getImg1Detail(UiObject2 u2) {
            List<String> list = new ArrayList<String>();
            List<UiObject2> listEle = u2.getChildren();
            String title = listEle.get(1).getChildren().get(0).getText() + "|" + listEle.get(1).getChildren().get(1).getText();
            String name = listEle.get(3).getText();
            String name1 = listEle.get(4).getText();
            List<UiObject2> list1 = listEle.get(5).getChildren();
            String price = list1.get(0).getText() + list1.get(1).getText() + list1.get(2).getText();
            list.add(0, title);
            list.add(1, name);
            list.add(2, name1);
            list.add(3, price);
            return list;
        }

        /**
         * 获取第二类单品
         * i 的值从 1到4
         *
         * @param i
         * @return
         */
        public UiObject2 getImg2Ele(int i) {
            UiObject2 u2 = getImgs().getChildren().get(i).getChildren().get(0);
            return u2;
        }

        /**
         * 获取第二类单品的详情
         * list 的第一个是 标题|副标题 第二个是 价格
         *
         * @param u2
         * @return
         */
        public List<String> getImg2Detail(UiObject2 u2) {
            List<String> list = new ArrayList<String>();
            List<UiObject2> listEle = u2.getChildren();
            String title = listEle.get(2).getText().trim() + "|" + listEle.get(3).getText().trim();
            String price = "xz";
            if (listEle.size() == 5) {
                List<UiObject2> u = listEle.get(4).getChildren();
                price = u.get(0).getText().trim() + u.get(1).getText().trim() + u.get(2).getText().trim();
            }
            list.add(0, title);
            list.add(1, price);
            return list;
        }
    }

    /**
     * 热门目的地模块
     */
    public static class HotDestination {
        private static HotDestination hotDestination;
        private UiDevice device;

        private HotDestination(UiDevice device) {
            this.device = device;
        }

        public static HotDestination getInstent(UiDevice device) {
            if (hotDestination == null) {
                hotDestination = new HotDestination(device);
            }
            return hotDestination;
        }
    }

    /**
     * 热门专题模块
     */
    public static class HotSpecial {
        private static HotSpecial hotSpecial;
        private UiDevice device;

        private HotSpecial(UiDevice device) {
            this.device = device;
        }

        public static HotSpecial getInstent(UiDevice device) {
            if (hotSpecial == null) {
                hotSpecial = new HotSpecial(device);
            }
            return hotSpecial;
        }
    }

    /**
     * 广告6+1模块
     */
    public static class GuangGao {
        private static GuangGao guangGao;
        private UiDevice device;

        private GuangGao(UiDevice device) {
            this.device = device;
        }

        public static GuangGao getInstent(UiDevice device) {
            if (guangGao == null) {
                guangGao = new GuangGao(device);
            }
            return guangGao;
        }

    }
}
